/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mail;

import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import static mail.Protocol.SMTPS;
import static mail.Protocol.TLS;
import mail.datamodel.IdDataSource;

/**
 *
 * @author damfercer
 */
@Stateless
public class EmailEJB{
    //@Override
    @Asynchronous
    public void sendEmail(Integer port, String host, String from, Boolean auth, final String username, final String password, Protocol protocol, Boolean debug, String to, String subject, String text) {
        Properties props=new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        switch (protocol){
            case SMTPS:
                props.put("mail.smtp.ssl.enable", true);
                break;
            case TLS:
                props.put("mail.smtp.starttls.enable", true);
                break;
        }
        
        Authenticator authenticator = null;
        if (auth) {
            props.put("mail.smtp.auth", true);
            authenticator = new Authenticator() {
                private PasswordAuthentication pa = new PasswordAuthentication(username, password);
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return pa;
                }
            };
        }
        Session session = Session.getInstance(props, authenticator);
        session.setDebug(debug);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            message.setRecipients(Message.RecipientType.TO, address);
            message.setSubject(subject);
            message.setSentDate(new Date());
            message.setText(text);
            Transport.send(message);
        } 
        catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }

    //@Override
    @Asynchronous
    public void sendHtmlEmail(Integer port, String host, String from, Boolean auth, final String username, final String password, Protocol protocol, Boolean debug, String to, String subject, String html, List<IdDataSource> dataSources) {
        Properties props=new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", port);
        switch (protocol){
            case SMTPS:
                props.put("mail.smtp.ssl.enable", true);
                break;
            case TLS:
                props.put("mail.smtp.starttls.enable", true);
                break;
        }
        
        Authenticator authenticator = null;
        if (auth) {
            props.put("mail.smtp.auth", true);
            authenticator = new Authenticator() {
                private PasswordAuthentication pa = new PasswordAuthentication(username, password);
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return pa;
                }
            };
        }
        Session session = Session.getInstance(props, authenticator);
        session.setDebug(debug);
        MimeMessage message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            message.setRecipients(Message.RecipientType.TO, address);
            message.setSubject(subject,"UTF-8");
            message.setSentDate(new Date());
            MimeMultipart multipart = new MimeMultipart("related");
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(html, "text/html; charset=utf-8");
            multipart.addBodyPart(messageBodyPart);
            if(dataSources!=null && !dataSources.isEmpty()){
                for (IdDataSource dataSource : dataSources) {
                    messageBodyPart = new MimeBodyPart();
                    messageBodyPart.setDataHandler(new DataHandler(dataSource.getDataSource()));
                    messageBodyPart.addHeader("Content-ID",dataSource.getId());
                    multipart.addBodyPart(messageBodyPart);
                }
            }
            /*message.setText(text);*/
            message.setContent(multipart);
            Transport.send(message);
        } 
        catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }
    
    
    
    @Asynchronous
    public void sendMail(String emailTo,String subject, String text){
        Integer port=587;
        String host="smtp.therecruitery.com";
        String from="no-reply@therecruitery.com";
        Boolean auth=true;
        String username="no-reply@therecruitery.com";
        String password="Naftalina9";
        Protocol protocol=Protocol.SMTP;
        Boolean debug=false;
        this.sendEmail(port, host, from, auth, username, password, protocol, debug, emailTo, subject, text);
    }
    
    @Asynchronous
    public void sendHtmlMail(String emailTo,String subject, String html, List<IdDataSource> dataSources){
        Integer port=587;
        String host="smtp.therecruitery.com";
        String from="no-reply@therecruitery.com";
        Boolean auth=true;
        String username="no-reply@therecruitery.com";
        String password="Naftalina9";
        Protocol protocol=Protocol.SMTP;
        Boolean debug=false;
        this.sendHtmlEmail(port, host, from, auth, username, password, protocol, debug, emailTo, subject, html, dataSources);
    }
}
